
package com.zackary.spring_blogs.util;

import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 分页查询
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2024/11/19 11:13
 */

public class PageQueryUtil extends LinkedHashMap<String,Object> {
    //当前页码
    private int page;
    //每一页的数据量
    private int limit;
    //构造器
    //初始化分页查询参数, 将键值对传入对象中
    //利用对象中参数,完成分页
    public PageQueryUtil(Map<String,Object> params){
        //利用父类方法,计算哈希值
        //利用哈希值查找对应桶,并遍历
        //相同则更新并返回旧值;无相同创建新节点并插入链表
        this.putAll(params);

        //分页参数
        this.page = Integer.parseInt(params.get("page").toString());
        this.limit = Integer.parseInt(params.get("limit").toString());
        this.put("start",(page - 1) * limit);
        this.put("page",page);
        this.put("limit",limit);
    }
    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    @Override
    public String toString() {
        return "PageUtil{" +
                "page=" + page +
                ", limit=" + limit +
                '}';
    }


}

